package marcis.jarina.appengine;


import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;


@Controller
@RequestMapping(value = "/csvloader")
public class CSVLoaderController {

    private static final Logger logger = Logger.getLogger(CSVLoaderController.class);

    @Autowired
    private CSVService csvService;

    @RequestMapping(value = "/first.html", method = RequestMethod.GET)
    public ModelAndView displayFirstTime() {
        ModelAndView modelAndView = new ModelAndView("loadResult");
        return modelAndView;
    }

    @RequestMapping(value = "/archive.html", method = RequestMethod.POST)
    public ModelAndView gallery(@RequestParam("file") MultipartFile file) throws IOException {
        logger.info("--- CSV loader controller start ---");
        ModelAndView modelAndView = new ModelAndView("loadResult");
        modelAndView.addObject("archives", csvService.getArchiveList(file.getInputStream()));
        logger.info("--- CSV loader controller finished ---");
        return modelAndView;
    }


}
